#!/usr/bin/env python
# -*- coding: utf-8 -*-
# 时间     : 2021-06-30 22:08
# 作者     : DL (584054432@qq.com)
# 网址     : https://gitee.com/dingliangPython/
# 软件版本 : Python3.7.3
# 功能     ：


import cx_Oracle
import os
import mysql_client
from mysql_client import PooledDB
import logging
from oracle_error import error_code

os.environ['NLS_LANG'] = 'AMERICAN_AMERICA.ZHS16GBK'


class Connect(mysql_client.Connect):
    """docstring for Connect"""

    def __init__(self, config):
        super(Connect, self).__init__(config)
        self.log = logging.getLogger('main.oracle_client')
        self.config = config
        # 连接池对象
        self.__pool = None
        self.error_code = error_code

    # 获取连接池连接
    def __get_pool_connect(self):
        """
        @summary: 静态方法，从连接池中取出连接
        @return MySQLdb.connection
        """
        try:
            if self.__pool is None:
                dsn = cx_Oracle.makedsn(self.config.get('host'), port = int(self.config.get('port', 1521)),
                                        service_name = self.config.get('database'))
                self.__pool = PooledDB(cx_Oracle, user = self.config.get('username'),
                                       password = self.config.get('password'), dsn = dsn, mincached = 1, maxcached = 20,
                                       )
                self.log.warning("初始化{}，连接池成功。。。".format(self.config.get('type')))
            return self.__pool.connection()
        except Exception as e:
            self.__pool = None
            self.get_error_info(e)

    def __get_odbc_connect(self):
        return self.__get_connect()

    # 获取数据库连接
    def __get_connect(self):
        try:
            dsn = cx_Oracle.makedsn(self.config.get('host'), port = int(self.config.get('port', 1521)),
                                    service_name = self.config.get('database'))
            connect = cx_Oracle.connect(user = self.config.get('username'), password = self.config.get('password'),
                                        dsn = dsn,
                                        )
            return connect
        except Exception as e:
            self.get_error_info(e)


if __name__ == '__main__':
    import time

    config = {
            'user': 'ygct',
            'mode': 'ODBC',
            'type': 'sql server',
            'name': 'ics',
            'host': '127.0.0.2',
            'dsn': 'ORACLE',
            'port': 1521,
            'password': 'ygct',
            'database': 'XE',
            # 'charset': 'gbk'
            }
    client = Connect(config)
    sql = "SELECT SYSDATE FROM DUAL "
    while 1:
        a = client.loadSql(sql)
        print(a)
        time.sleep(3)